const attrService = require('../service/attrService')
const categoryService = require('../service/categoryService')
const {dataHandler} = require('../utils/handle-data')
class CategoryController {
  async getCateList(ctx, next) {
    const res = await categoryService.getCateList()
    const data = dataHandler(res, 'tree')
    ctx.body = {
      data,
      meta: {
        msg: "获取成功",
        status:200
      }
    }
  }
  async addCate(ctx, next) {
    const { pid, name, level } = ctx.request.body 
    console.log(pid,name,level);
    const { insertId } = await categoryService.addCate(pid, name, level)
    const res = await categoryService.getCateById(insertId)
    ctx.body = {
      data: res[0],
      meta: {
        msg: '创建成功',
        status:201
      }
    }
  }
  async getCateById(ctx, next) {
    const { id } = ctx.params 
    const res = await categoryService.getCateById(id)
    if (res.length === 0) {
      ctx.body = {
        data:null,
        meta: {
          msg: '获取失败',
          status:404
        }
      }
    } else {
      ctx.body = {
        data: res[0],
        meta: {
          msg: '获取成功',
          status:200
        }
      }
    }
    
  }
  async updateCate(ctx, next) {
    const { id } = ctx.params
    const {name} = ctx.request.body
    await categoryService.updateCateById(name, id)
    const data = await categoryService.getCateById(id)
    ctx.body = {
      data:data[0],
      meta: {
        msg: '更新成功',
        status:200
      }
    }
  }
  async removeCateById(ctx, next) {
    const { id } = ctx.params
    const res = await categoryService.removeCateById(id)
    if (res.affectedRows === 1) {
      ctx.body = {
        data: null,
        meta: {
          msg: '删除成功',
          status:200
        }
      }
    } else {
      ctx.body = {
        meta: {
          msg: '删除失败',
          status:400
        }
      }
    }
  }
  async getAttr(ctx, next) {
    const { id} = ctx.params
    const { sel } = ctx.query
    const res = await attrService.getCateAttr(id, sel)
    if (res.length === 0) {
      ctx.body = {
        data: null,
        meta: {
          msg: '获取失败，参数不存在',
          status:400
        }
      }
    } else {
      ctx.body = {
        data: res,
        meta: {
          msg: '获取成功',
          status:200
        }
      }
    }
  }
  async addAttr(ctx, next) {
    const {id} = ctx.params
    const {attr_name,attr_sel,attr_vals} = ctx.request.body
    const {insertId} = await attrService.addAttr(id,attr_name,attr_sel,attr_vals)
    const res = await attrService.getAttr(insertId)
    ctx.body = {
      data: res[0],
      meta: {
        msg: '创建成功',
        status:201
      }
    }
  }
  async removeAttr(ctx, next) {
    const { cat_id, attr_id } = ctx.params 
    const res = await attrService.removeAttr(cat_id, attr_id)
    if (res.affectedRows === 1) {
      ctx.body = {
        data: null,
        meta: {
          msg: '删除成功',
          status:200
        }
      }
    } else {
      ctx.body = {
        meta: {
          msg: '删除失败',
          status:400
        }
      }
    }
  }
  async getAttrById(ctx, next) {
    const { cat_id, attr_id } = ctx.params
    const res = await attrService.getAttrById(cat_id, attr_id)
    if (res.length !== 0) {
      ctx.body = {
        data: res[0],
        meta: {
          msg: '获取成功',
          status:200
        }
      }
    } else {
      ctx.body = {
        data: null,
        meta: {
          msg: '获取失败，参数不存在',
          status:400
        }
      }
    }
  }
  async updateAttr(ctx, next) {
    const { cat_id, attr_id } = ctx.params
    const {attr_name,attr_sel,attr_vals} = ctx.request.body
    await attrService.updateAttr(cat_id, attr_id, attr_name, attr_sel, attr_vals)
    const data = await attrService.getAttrById(cat_id,attr_id)
    ctx.body = {
      data: data[0],
      meta: {
        msg: "更新成功",
        status:200
      }
    }
  }
}
module.exports = new CategoryController()